import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";
import dayjs from "dayjs";

Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor'],
  }],
  properties: {
    product: {
      type: Object
    },
    price: {
      type: Array
    },
    beginDay: {
      type: String
    }
  },
  data: {
    previewList: [],
    priceList: [],
    curSelect: '',
    dialogShow: false
  },
  observers: {
    price: function (price) {
      console.log('price', price)
      if (!price) {
        return;
      }
      this.parsePrice(price);
    },
  },
  methods: {
    handleSelect(e) {
      let detail = e.currentTarget.dataset.detail;
      if (!detail.enable) return;
      this.setData({ beginDay: detail.day, dialogShow: false });
      this.triggerEvent('select', detail.day)
      this.resetPreview(detail)
    },
    handleShowAll() {
      this.setData({ dialogShow: true })
    },
    handleClose() {
      this.setData({ dialogShow: false })
    },
    handleStop() {
      return true;
    },

    resetPreview(detail) {
      console.log('resetPreview--',detail)
      if (this.data.priceList.length <= 3) {
        return;
      }
      let beginDay = detail.day
      let previewList = this.data.previewList
      if (previewList[0].day !== beginDay && previewList[1].day !== beginDay
        && previewList[2].day !== beginDay) {
        previewList[2] = detail;

      } else {
        if (previewList[2].day !== beginDay && previewList[2].day !== this.data.priceList[2].day) {
          previewList[2] = this.data.priceList[2];
        }
      }
      this.setData({ previewList: previewList });
      console.log("previewList", this.data.previewList)
    },
    parsePrice(price) {
      console.info("price", price)
      if (price.length === 0) {
        this.setData({ priceList: [], previewList: [], curSelect: '' })
        return;
      }
      let weeks = ['日', '一', '二', '三', '四', '五', '六']
      let priceList = [];
      let firstEnable = null;
      let beginDay = '';
      for (let i = 0; i < price.length; i++) {
        let time = dayjs(price[i].day, 'YYYY-MM-DD')
        let detail = {
          md: time.format('MM-DD'),
          week: "周" + weeks[time.day()],
          ...price[i]
        }
        if (!firstEnable && detail.enable) {
          firstEnable = detail;
          beginDay = detail.day;
        }
        if (detail.state != 2) { priceList.push(detail) }

      }
      let previewList;
      console.log('priceList--',priceList)
      if (priceList.length >= 3) {
        previewList = [priceList[0], priceList[1], priceList[2]]
      } else {
        previewList = priceList;
      }
      // 预览前3个都是可用状态, 把可用的放在第三个位置
      if (previewList.length === 3 && previewList.filter(i => i.enable).length === 0 && firstEnable) {
        previewList[2] = firstEnable
      }
      this.triggerEvent('select', beginDay);
      this.setData({ priceList, previewList, beginDay });
      console.log("previewList", this.data.previewList)
    }
  }
});
